package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.IOrderSettingListService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/ordersettinglist")
public class OrderSettingListController {

    @Reference
    private IOrderSettingListService orderSettingListService;

    // 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = null;
        try {
            pageResult = orderSettingListService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pageResult;
    }

    // 添加功能
    @RequestMapping("/add")
    public Result add(@RequestBody Map map,Integer[] setmealIds){

        try {
            Result result = orderSettingListService.add(map,setmealIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  new Result(false,MessageConstant.ORDER_FAIL);
    }

    // 确认预约
    @RequestMapping("/confirm")
    public Result confirm( String telephone){

        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            return new Result(true,MessageConstant.CONFIRM_ORDER_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.CONFIRM_ORDER_FAIL);
        }
    }

    // 取消预约
    @RequestMapping("/cancel")
    public Result cancel(@RequestBody Map map){

        try {
            orderSettingListService.cancel(map);
            return new Result(true,MessageConstant.CANCEL_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.CANCEL_ORDER_FAIL);
        }

    }

    // 批量取消预约
    @RequestMapping("/batchCancel")
    public Result batchCancel(Integer[] orderIds){

        try {
            orderSettingListService.batchCancel(orderIds);
            return new Result(true,MessageConstant.BATCHCANCEL_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.BATCHCANCEL_ORDER_FAIL);
        }

    }
}
